angular.module('partnerApp').controller('partnerRegisterController', ['$scope', '$compile', '$rootScope', '$routeParams', function ($scope, $compile, $rootScope, $routeParams) {

    $scope.mobile = '';
    $scope.name = '';
    $scope.code = '';
    $scope.password = '';
    $scope.confimPassword = '';

    if (window.smsTimeDownId) {
        clearTimeout(window.smsTimeDownId);
        delete window.smsTimeDownId;
    }


    $scope.register = function () {
        if (!isMobile($scope.mobile)) {
            mui.toast('请输入正确的手机号');
            return;
        }
        if (!/^[0-9A-Za-z]{4}$/.test($scope.code)) {
            mui.toast('请输入正确的验证码');
            return;
        }

        if ($scope.name == '') {
            mui.toast('请输入姓名');
            return;
        }

        if ($scope.password == '') {
            mui.toast('请输入密码');
            return;
        }

        if ($scope.confimPassword == '') {
            mui.toast('请输入确认密码');
            return;
        }

        if ($scope.confimPassword != $scope.password) {
            mui.toast('两次输入密码不一致');
            return;
        }

        ajaxRequest(getRequestData("partnerRegister", {
            mobile: $scope.mobile,
            name: $scope.name,
            code: $scope.code,
            password: $scope.password,
            city: '200000',
            marketingNumber: getQueryString('marketNumber')
        }), function () {
            layer.msg("恭喜您！注册成功");
            setTimeout($scope.toLogin, 1500);
        });
    };

    /*发送验证码*/
    $scope.sendMsg = function () {
        if (!isMobile($scope.mobile)) {
            mui.toast('请输入正确的手机号');
            return;
        }
        ajaxRequest(getRequestData("sms", {
            mobile: $scope.mobile
        }), function () {
            mui.toast("发送成功");
            mui('#btnSendMsg').button('loading');
            timeDown(60);
        });

    };


    function timeDown(time) {
        if (time > 0) {
            jQuery('#btnSendMsg').html('重新发送(' + time + ')');
            window.smsTimeDownId = setTimeout(function () {
                timeDown(--time);
            }, 1000);
        } else {
            delete window.smsTimeDownId;
            mui('#btnSendMsg').button('reset');
            jQuery('#btnSendMsg').html('重新发送');
        }
    }

    $scope.toLogin = function () {
        if (is_weixn()) { // 微信
            location.replace('http://yirenyiti.gaozhikeji.com/partner-api/wx/entrance?type=http%3A%2F%2Fyirenyiti.gaozhikeji.com%2Fpartner-op');
        } else { //非微信
            location.replace('http://yirenyiti.gaozhikeji.com/partner-op/');
        }
    };

}]);